package com.soft.javase.day15.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/27
 */
public class TestRunner {
    public static void runTest(Class<?> testClass) {
        System.out.println("运行测试类: " + testClass.getSimpleName());

        int tests = 0;
        int passed = 0;
        int failed = 0;

        try {
            Object testInstance = testClass.getDeclaredConstructor().newInstance();

            for (Method method : testClass.getDeclaredMethods()) {
                if (method.isAnnotationPresent(MyTest.class)) {
                    tests++;
                    try {
                        method.invoke(testInstance);
                        System.out.println("✅ 测试通过: " + method.getName());
                        passed++;
                    } catch (Exception e) {
                        System.out.println("❌ 测试失败: " + method.getName() +
                                "  原因: " + e.getCause());
                        failed++;
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("无法创建测试实例");
        }

        System.out.println("测试结果: 总共 "+ tests +", 通过 "+passed+", 失败 "+failed);
    }
}
